﻿@using Seal.Model;
@{
    ReportTask task = Model;

    task.TemplateDescription = "Zip a folder or a file. Unzip a file to a folder.";

    //Parameters for this task
    task.Parameters.Add(new Parameter() { Name = "action", Value = "UNZIP", DisplayName = "Action", Description = "Zip/Unzip action to perform.", Enums = new string[] { "UNZIP|Unzip a file", "ZIPFILE|Zip a file", "ZIPFOLDER|Zip a folder" }, UseOnlyEnumValues = true });
    task.Parameters.Add(new Parameter() { Name = "zip_path", Value = "", DisplayName = "Zip file path", Description = "For Unzip: The Zip source file path (*.zip), for Zip: The Zip target file path." + ReportTask.TranslatedParameterDescriptionFull, Enums = new string[] { @"%SEALREPOSITORY%/Databases/excel samples.zip", ReportTask.ParentExecResultKeyword, ReportTask.ExecInputKeyword }, UseOnlyEnumValues = false });
    task.Parameters.Add(new Parameter() { Name = "password", Value = "", DisplayName = "Password", Description = "The password used to zip or unzip."+ ReportTask.TranslatedParameterDescription });
    task.Parameters.Add(new Parameter() { Name = "folder", Value = "", DisplayName = "Folder", Description = "For Unzip: The target folder used to unzip the file, for Zip a folder: the source folder." + ReportTask.TranslatedParameterDescriptionFull, Enums = new string[] { @"%SEALREPOSITORY%/TestFiles/ZipFolder", ReportTask.ParentExecResultKeyword, ReportTask.ExecInputKeyword }, UseOnlyEnumValues = false });
    task.Parameters.Add(new Parameter() { Name = "source_path", Value = "", DisplayName = "Source file path", Description = "For Zip a file: The source file path to zip." + ReportTask.TranslatedParameterDescriptionFull, Enums = new string[] { @"%SEALREPOSITORY%/Databases/Translations.csv", ReportTask.ParentExecResultKeyword, ReportTask.ExecInputKeyword }, UseOnlyEnumValues = false });
    task.Parameters.Add(new Parameter() { Name = "filter", Value = "*.*", DisplayName = "File filter", Description = "For Zip a folder: The file filter apply to get the files.", Enums = new string[] { @"*.*", @"*.xlsx", @"*.csv", @"*.txt" }, UseOnlyEnumValues = false });
    task.Parameters.Add(new Parameter() { Name = "recursive", BoolValue = true, DisplayName = "Is recursive", Description = "For Zip a folder: If true, sub-folders are zipped recursively." });


    task.Script = @"@using System.IO
@{
    ReportTask task = Model;
    Report report = task.Report;
    
    var action = task.GetValue(""action"");
    var zipPath = task.GetValueTranslated(""zip_path"");
    var password = task.GetValueTranslated(""password"");
    var folder = task.GetValueTranslated(""folder"");
    var sourcePath = task.GetValueTranslated(""source_path"");

    if (string.IsNullOrEmpty(folder)) {
        throw new Exception(""Folder is empty."");
    }

    if (action == ""UNZIP"") {    
        if (!File.Exists(zipPath)) {
            throw new Exception(""Invalid zip path: {zipPath}"");
        }
        task.LogMessage($""Unzipping '{zipPath}' to '{folder}'"");
        if (!Directory.Exists(folder)) {
            Directory.CreateDirectory(folder);
        }
         FileHelper.ExtractZipFile(zipPath, password, folder);
        task.ExecResult = folder;
    }
    else if (action == ""ZIPFILE"") {
        if (!File.Exists(sourcePath)) {
            throw new Exception(""Invalid source path: {sourcePath}"");
        }
        task.LogMessage($""Zipping '{sourcePath}' to '{zipPath}'"");
        FileHelper.ExtractZipFile(zipPath, password, folder);
        task.ExecResult = zipPath;
    }
    else if (action == ""ZIPFOLDER"") {    
        if (!Directory.Exists(folder)) {
            throw new Exception(""Invalid folder: {folder}"");
        }
        task.LogMessage($""Zipping '{folder}' to '{zipPath}'"");
        FileHelper.CreateZIPFromFolder(folder, task.GetValue(""filter""), task.GetBoolValue(""recursive"", true), zipPath, password);
        task.ExecResult = zipPath;
    }
}
";

    task.BodyScript = ReportTask.BodyScriptTemplate;
}
